package com.icebergteam.warfacewidget.core;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

public abstract class Parser extends Object  {
	final public static String className = Parser.class.getCanonicalName();
	
	public static final int REQUEST_COMPLETED = 1;
	public static final int REQUEST_FAILED = REQUEST_COMPLETED << 1;
	public static final int LIST_RESTORED = REQUEST_FAILED << 1;
	public static final int XML_INVALID = LIST_RESTORED << 1;
	public static final int DB_INVALID = XML_INVALID << 1;
	public static String KEY_INTENT_RESTORE = String.format("%s.KEY_RESTORE",
			className);
	public static final int TIMEOUT = 10000;
	protected Document doc;
	protected int stateRequest;
	protected boolean restoreKey;
	
	public static String KEY_INTENT_RESULT = String.format("%s.KEY_RESULT",
			className);
	
	public Parser(boolean restoreKey) {
		this.setRestoreKey(restoreKey);
	}

	public Parser() {
	}

	abstract public int saveData(String url) throws Exception;

	abstract protected void restore(int complete, int failed)	throws Exception;
	
	abstract public Object  parseXML(Document doc, int limit) throws Exception;
	
	abstract public Object  parseXML(JSONObject doc, int limit) throws Exception;
	
	abstract protected Object getItem(Element elem) throws Exception;

	protected int parseInt(String str){
		Pattern intsOnly = Pattern.compile("[0-9]+");
		
		Matcher makeMatch = intsOnly.matcher(str);
		makeMatch.find();
		
		return Integer.parseInt(makeMatch.group());
	}

	public int getStateRequest() {
		return this.stateRequest;
	}

	
	public boolean isRestoreKey() {
		return this.restoreKey;
	}

	public void setRestoreKey(boolean m_restoreKey) {
		this.restoreKey = m_restoreKey;
	}

	public Document getDoc() {
		return this.doc;
	}

	public void setDoc(Document m_doc) {
		this.doc = m_doc;
	}


}